package com.mindainfo.groundwave.server.equipment.service;

import com.mindainfo.groundwave.common.api.equipment.param.RelayQueryListParam;
import com.mindainfo.groundwave.common.api.equipment.service.IRelayService;
import com.mindainfo.groundwave.common.api.equipment.vo.RelayQueryListVo;
import com.mindainfo.groundwave.common.api.member.service.IDeptService;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.ResultFactory;
import com.mindainfo.groundwave.common.bean.TableData;
import com.mindainfo.groundwave.server.equipment.bean.RelayInfo;
import com.mindainfo.groundwave.server.equipment.bean.RelayInstall;
import com.mindainfo.groundwave.server.equipment.mapper.RelayInfoMapper;
import com.mindainfo.groundwave.server.equipment.mapper.RelayInstallMapper;
import com.mindainfo.groundwave.server.equipment.mapper.RelayStateMapper;
import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MD-01 on 2017/7/5.
 */
@Service
@Transactional
public class RelayServiceImpl implements IRelayService {

    @Resource
    private RelayInfoMapper relayInfoMapper;
    @Resource
    private RelayInstallMapper relayInstallMapper;
    @Resource
    private RelayStateMapper relayStateMapper;
    @Resource
    private IDeptService deptService;

    /**
     * 页面查询列表
     */
    public CommonResult<TableData<RelayQueryListVo>> queryList(RelayQueryListParam param){

        //获取当前用户的部门树
        CommonResult<List<Integer>> myDeptIdList = deptService.getMyDeptIdList();

        List<RelayInfo> relayInfos = relayInfoMapper.queryList(param.getStart(),
                param.getNum(), param.getKey(),myDeptIdList.getData());
        int count = relayInfoMapper.queryListCount(param.getKey(),myDeptIdList.getData());

        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd");
        List<RelayQueryListVo> vos = new ArrayList<>();
        for(RelayInfo info : relayInfos){
            RelayQueryListVo vo = new RelayQueryListVo();
            vo.setRelayId(info.getId());
            vo.setDeviceId(info.getDeviceId());
            vo.setVendor(info.getVendor());
            vo.setRealyVersion(info.getVersion());
            vo.setMakerTime(format.format(info.getMakerTime()));

            RelayInstall relayInstall = relayInstallMapper.queryByRelayId(info.getId());
            if(relayInstall!=null){
                vo.setInstallPersion(relayInstall.getInstallPersion());
                vo.setInstallTime(format.format(relayInstall.getInstallTime()));
                vo.setLocation(relayInstall.getLocation());
                vo.setLng(relayInstall.getLng());
                vo.setLat(relayInstall.getLat());
            }
            vo.setPower(80);
            vo.setState("ok");
            vos.add(vo);
        }
        TableData<RelayQueryListVo> data = new TableData<>();
        data.setRecordsFiltered(count);
        data.setRecordsTotal(count);
        data.setData(vos);

        return ResultFactory.ok(data);
    }
}
